﻿Imports System.Data.OleDb
Imports System.Data
Partial Class PlacesViewPlacesDetails
    Inherits System.Web.UI.Page

    Protected Sub DVPlaces_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.DetailsViewPageEventArgs) Handles DVPlaces.PageIndexChanging
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("myFirstDatabase").ConnectionString)
        Dim SqlString As String = "Insert into tblPlaceTagJun(PlaceID,PlaceTag) Values(@f1,@f2)"
        oleDbConn.Open()


        For Each cb As ListItem In cbListTag.Items
            If cb.Selected Then
                'here is where the join to the table of tags and articles are added
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        Dim newTags = tbTag.Text.Split(",")
        For Each newTag In newTags
            'added new tag here...
            Dim newTagSql = "Insert INTO tblTagPlace(Tag) VALUES(@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()

            'finding if of new tag
            Dim getLastIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID from TblTagPlace ORDER BY ID DESC", oleDbConn)
            Dim dr = getLastIdCmd.ExecuteReader()
            dr.Read()
            Dim lastID = dr.GetValue(0)

            'associate this last id with the picture id
            Dim placecmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            placecmd.CommandType = CommandType.Text
            placecmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            placecmd.Parameters.AddWithValue("@f2", lastID)
            placecmd.ExecuteNonQuery()




        Next

    End Sub

    Protected Sub btnComment_Click(sender As Object, e As System.EventArgs) Handles btnComment.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("myFirstDatabase").ConnectionString)
        Dim SqlString As String = "Insert into tblPlacesComments(title,comment,SubmittedDate,Placeid) Values (@f1,@f2,@f3,@f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim SubmittedDate As DateTime = Now()
        Dim format As String = "ddd MMM yyyy"
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", txtTitle.Text)
        cmd.Parameters.AddWithValue("@f2", txtcontent.Text)
        cmd.Parameters.AddWithValue("@f3", SubmittedDate.ToString(format))
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
    End Sub


    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("myFirstDatabase").ConnectionString)
            oleDbConn.Open()
            Dim PlaceViewSql As String = "INSERT INTO tblPlacesViewed(Neighbor, Place) VALUES(@f1,@f2)"
            Dim cmd As OleDbCommand = New OleDbCommand(PlaceViewSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
            cmd.ExecuteNonQuery()
        End If
    End Sub
End Class
